<?php
namespace app\admins\controller;

use app\BaseController;
use app\Request;
use app\validate\AdminCheck;
use think\exception\ValidateException;
use think\facade\Db;

class admin extends BaseController
{

    protected $middleware = [\app\middleware\Check::class];
//    渲染视图
    public function index()
    {
       return view();
    }

    public function select()
    {
        if(request()->isGet()){
            $data = input('get.');
            $list = Db::name('admin')->where('id',$data['id'])->find();
            return json($list);
        }

    }

//    删除用户
    public function delete()
    {
        if(request()->isGet()){
            $data = input('get.');
            Db::name('admin')->where('id',$data['id'])->delete();

        }

    }

//    修改用户
    public function edit(){
        $data = request()->param();
//        try {
//            $res = validate(AdminCheck::class)->check([
//                'email' => $data['email'],
//                'mobile' => $data['mobile']
//            ]);
            $admin = \app\model\Admin::find($data['id']);
            $admin->mobile   = $data['mobile'];
            $admin->email    = $data['email'];
            $admin->role    = $data['role'];
            $ret = $admin->save();
            if($ret){
                return  json(["state"=>200]);
            }
            else{
                return json(["state"=>204,"message"=>'修改信息失败']);
            }

//        } catch (ValidateException $e) {
//            return  json(["state"=>204,"message"=>$e->getError()]);
//        }

    }


//    获取本地ip
    function getRealIP(){
        $forwarded = request()->header("x-forwarded-for");
        if($forwarded){
            $ip = explode(',',$forwarded)[0];
        }else{
            $ip = request()->ip();
        }
        return $ip;
    }


//    修改用户状态
    public function editState(){
        $data = request()->param();
        $admin = \app\model\Admin::find($data['id']);
        $admin->state = $data['state'];
        $ret = $admin->save();
        if($ret){
            return  json(["state"=>200]);
        }
        else{
            return json(["state"=>204,"message"=>'修改状态失败']);
        }
    }

    //    分页展示
    public function article(Request $request){

//        查询条件
        $where = [];
        if (!empty($request->param('name'))) {
            $where[] = ['name|id|role', 'like', "%{$request->param('name')}%"];
        }
        //获取每页显示的条数
        $limit= $request->param('limit');
        //获取当前页数
        $page= $request->param('page');
        $list = Db::name('admin')
            ->where($where)
            ->paginate($limit,false,['page'=>$page])
            ->toArray();
        return json(['code'=>0,'msg'=>'','count'=>$list['total'],'data'=>$list['data']]);

    }

    //    添加用户
    public function addAdmin(){
        $data = request()->param();
        try {
            $res = validate(AdminCheck::class)->check($data);
             $data1 = [
                 'name' => $data['name'],
                'password' =>  md5('dytsyq'.$data['password']),
                'email'    => $data['email'],
                'mobile'     => $data['mobile'],
                'create_at' => $data['create_at'],
                 'login_ip' => $this->getRealIP(),
                 'login_at' => $data['create_at'],
                 'role' => '主管'
            ];
             $Db = Db::name('admin');
             if($Db->where('name',$data['name'])->find()) {
                 return  json(["state"=>204,"message"=>"用户名不能重复"]);
             }
              $ret = $Db->insert($data1);
//             return $Db->getLastSql();
            if($ret){
                return  json(["state"=>200]);
            }
            else{
                return json(["state"=>204,"message"=>'添加用户失败']);
            }

        } catch (ValidateException $e) {
            return  json(["state"=>204,"message"=>$e->getError()]);
        }

    }

}
